define([
    "esri/layers/FeatureLayer"
], function(FeatureLayer) {
    var featureLayer = FeatureLayer.createSubclass({
        setData: function(mapView, layer) {
            var me = this;
            if (!mapView) {
                return
            };
            let mapUtils = mapView.mapUtils;
            var options = layer.layer || {};
            var data = options.data || [];
            data.where = data.where || '1=1';
            me.definitionExpression = data.where + mapUtils.getWhereRandom();

            data.renderer = data.renderer || {};
            data.renderer.symbol = data.renderer.symbol || options.symbol;
            data.renderer.symbol = me.setSymbol(data.renderer.symbol);
            if (data.renderer.type) {
                me.__setRenderer(mapView, mapUtils, layer, data.renderer);
            };

            if (typeof options.callback == 'function') {
                options.callback(layer);
            };
        },
        setSymbol: function(symbol) {
            if (symbol && symbol.outline && symbol.outline.style) {
                if (symbol.outline.style == 'dashdot') {
                    symbol.outline.style = 'dash-dot';
                }
            }
            return symbol;
        },
        setExtent: function(mapView, options) {
            var me = this;
            if (!mapView) {
                return
            };

            var where = "1=1";
            if (options.data && options.data.where) {
                where = options.data.where;
            };

            options.expandFactor = options.expandFactor || 2;

            mapView.zoomToCenterByLayer(me.id, {
                where: where,
                zoomType: 'extent',
                isClick: false,
                ...options,
                callback: function(graphics, center) {
                    if (typeof options.callback == "function") {
                        me.___layer.___data = graphics;
                        me.___layer.___center = center;
                        options.callback(me.___layer);
                    };
                }
            });
        },
        setWhere: function(where) {
            var me = this;
            me.setDefinitionExpression(where + me.mapView.mapUtils.getWhereRandom());
        },
        __setRenderer: function(mapView, mapUtils, layer, options) {
            var me = this;
            var renderer = mapUtils.createRenderer(mapView, options);
            me.renderer = renderer;
        },
        getGraphicLocation: function(mapView, esriEvent, mapUtils) {
            var me = this;
            var location = null;

            var _geometryType = esriEvent.graphic.geometry.type;
            switch (_geometryType) {
                case mapUtils.Enum.GType.Point:
                    location = me.getPointGraphicLocation(mapView, esriEvent);
                    break;
                case mapUtils.Enum.GType.Polyline:
                    location = me.getPolylineGraphicLocation(mapView, esriEvent);
                    break;
                case mapUtils.Enum.GType.Extent:
                case mapUtils.Enum.GType.Circle:
                case mapUtils.Enum.GType.Polygon:
                    location = me.getPolygonGraphicLocation(mapView, esriEvent);
                    break;
            };
            return location;
        },
        getPointGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.graphic) {
                return;
            }
            var geometry = esriEvent.graphic.geometry || {};

            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.longitude;
            location[latFieldName] = geometry.latitude;
            return location;
        },
        getPolylineGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.mapPoint) {
                return;
            }
            var geometry = esriEvent.mapPoint || {};

            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {};
            location[lngFieldName] = geometry.longitude;
            location[latFieldName] = geometry.latitude;
            return location;
        },
        getPolygonGraphicLocation: function(mapView, esriEvent) {
            if (!esriEvent || !esriEvent.mapPoint) {
                return;
            }
            var geometry = esriEvent.mapPoint || {};
            var lngFieldName = mapView.getLngFieldName();
            var latFieldName = mapView.getLatFieldName();
            var location = {}
            location[lngFieldName] = geometry.longitude;
            location[latFieldName] = geometry.latitude;
            return location;
        }
    });
    return featureLayer;
});